package com.example.demo;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
@Slf4j
public class EmpService {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Transactional
    public void insert(){
        jdbcTemplate.update("insert into t_student(name) values('aaa')");
    }

    @Transactional(rollbackFor = RuntimeException.class)
    public void backInsert(){
        jdbcTemplate.update("insert into t_student(name) values('bbb')");
        throw new RuntimeException();
    }

    public void invokeBackInsert(){
        jdbcTemplate.update("insert into t_student(name) values('ccc')");
    }
    public void findAll(){
        jdbcTemplate.queryForList("select * from t_student").forEach(m->log.info(m.toString()));
    }
}
